package com.agileai.crm.module.procustomer.handler;

import java.util.Date;

import com.agileai.common.KeyGenerator;
import com.agileai.crm.common.PrivilegeHelper;
import com.agileai.crm.module.procustomer.service.OrgInfoManage;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.bizmoduler.core.StandardService;
import com.agileai.hotweb.controller.core.StandardEditHandler;
import com.agileai.hotweb.domain.FormSelectFactory;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.util.DateUtil;

public class OrgInfoCreateCustomerEditHandler extends StandardEditHandler {
	public OrgInfoCreateCustomerEditHandler() {
		super();
		this.serviceId = buildServiceId(OrgInfoManage.class);
	}
	public ViewRenderer prepareDisplay(DataParam param) {
		String operaType = param.get(OperaType.KEY);
		param.put("ORG_ID", param.get("orgId"));
		DataRow record = getService().getRecord(param);
		DataParam showParam = new DataParam();
		showParam.put("CUST_NAME",record.get("ORG_NAME"));
		showParam.put("CUST_INTRODUCE",record.get("ORG_INTRODUCTION"));
		record.putAll(showParam);
		this.setAttributes(record);	
		this.setOperaType(operaType);
		processPageAttributes(param);
		return new LocalRenderer(getPage());
	}
	protected void processPageAttributes(DataParam param) {
	 	setAttribute("CUST_STATE",
                FormSelectFactory.create("CUST_STATE")
                                 .addSelectedValue(getAttributeValue("CUST_STATE",
                                                                          "init")));
		setAttribute("CUST_LEVEL", FormSelectFactory.create("CUST_LEVEL")
				.addSelectedValue(getOperaAttributeValue("CUST_LEVEL", "")));
		setAttribute("CUST_PROGRESS_STATE", FormSelectFactory.create("CUST_PROGRESS_STATE")
				.addSelectedValue(getOperaAttributeValue("CUST_PROGRESS_STATE", "")));
		User user = (User) this.getUser();
		this.setAttribute("CUST_CREATE_NAME",
				this.getAttribute("CUST_CREATE_NAME", user.getUserName()));
		this.setAttribute("CUST_CREATE_ID",
				this.getAttribute("CUST_CREATE_ID", user.getUserId()));
		String ccrtTime = (String) this.getAttribute("CUST_CREATE_TIME",
				DateUtil.getDateByType(DateUtil.YYMMDDHHMI_HORIZONTAL,
						new Date()));
		this.setAttribute("CUST_CREATE_TIME", ccrtTime);
	}
	@PageAction
	public ViewRenderer createCustomer(DataParam param) {
		User user = (User) getUser();
		PrivilegeHelper privilegeHelper = new PrivilegeHelper(user);
		String custId = KeyGenerator.instance().genKey();
		((OrgInfoManage) getService()).createCustomerRecord(param,custId);
		if(privilegeHelper.isSalesMan()){
			DataParam salesManParam = new DataParam("custId",custId,"userId",user.getUserId());
			((OrgInfoManage) getService()).addSalesManRelation(salesManParam);	
		}
		String responseText = param.get("CUST_ID");
		return new AjaxRenderer(responseText);
	}
	protected StandardService getService() {
		return (StandardService) this.lookupService(this.getServiceId());
	}
}
